package com.sherlocky.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;

public class HttpUtils {
	/**
     * 中文编码转换, 典型的情况是下载中文名的文件时, 浏览器不能正确地显示汉字
     */
    public static String convert(HttpServletRequest request, String fileName) {
        if (fileName == null) {
            throw new IllegalArgumentException("输入参数是null");
        }
        try{
        	String agent = request.getHeader("USER-AGENT");  
        	if (null != agent && -1 != agent.indexOf("MSIE")) {  
        		return URLEncoder.encode(fileName, "UTF8");  
        	}else if (null != agent && -1 != agent.indexOf("Mozilla")) {  
        		return "=?UTF-8?B?"+(new String(Base64.encodeBase64(fileName.getBytes("UTF-8"))))+"?=";  
        	} else {  
        		return fileName;  
        	}
        }catch(UnsupportedEncodingException ex){
        	return fileName;
        }
    }
}
